home *** CD-ROM | disk | FTP | other *** search
- /* DisplayAuditFile.c */
- /*
- * DisplayAuditFile.c
- * Copyright © 1992-93, Apple Computer Inc. All Rights Reserved.
- * Output file writer for the Audit library.
- * Edit History
- * 93.01.14 MM Removed Think C stdio dependencies. Added Standard Fil
- * dialog with additional dialog items to allow defining the
- * audit selector.
- * 93.01.19 MM MPW \r is the wrong character. Force <returns> on both MPW and
- * Think, because this is what the Mac file system requires for
- * end of line. (Change is in DisplayAuditFile.c)
- * 93.07.09 MM Reformatted. CloseOutputFile now takes file refNum, volume
- * refNum and filename parameters, rather than a SFReply.
- */
- #include "DisplayAudit.h"
- #define kEndOfLine 0x0D /* Return */
-
-
- /*
- * File Output
- * Open/create a file to write the audit information. reply.good is TRUE on
- * success, FALSE if the user cancels the SFPutFile. Serious errors fail via the
- * failure mechanism. The file is TEXT type. The creator is TeachText. On success:
- * reply.vRefNum The volume id.
- * fileRefNum The file id.
- */
- void
- PromptAndCreateAuditOutputFile(
- ConstStr255Param promptString,
- ConstStr255Param defaultFileName,
- OSType creator,
- short *fileRefNum,
- short *volumeRefNum,
- Str63 fileName
- )
- {
- Point where;
- DialogTHndl dialog;
- Rect box;
- SFReply reply;
-
- /*
- * Center the dialog
- */
- dialog = (DialogTHndl) GetResource('DLOG', putDlgID);
- if (dialog == NULL) /* No resource! */
- SetPt(&where, 80, 80);
- else {
- box = (**dialog).boundsRect; /* Dialog shape */
- ReleaseResource((Handle) dialog);
- where.h =
- (width(qd.thePort->portRect) - width(box)) / 2;
- where.v =
- ((height(qd.thePort->portRect) - GetMBarHeight()) / 3)
- + GetMBarHeight();
- }
- SFPutFile(where, promptString, defaultFileName, NULL, &reply);
- if (reply.good == FALSE)
- *fileRefNum = 0;
- else {
- FailOSErr(
- CreateOutputFile(creator, reply.fName, reply.vRefNum, fileRefNum),
- kErrCreateOutputFile
- );
- pstrcpy(fileName, reply.fName);
- *volumeRefNum = reply.vRefNum;
- }
- }
-
- /*
- * Create an output file. Return noErr on success. On success, fileRefNum has the
- * refNum. On Failure, it has zero.
- */
- OSErr
- CreateOutputFile(
- OSType creator,
- ConstStr255Param fileName,
- short vRefNum,
- short *fileRefNum
- )
- {
- OSErr status;
-
- /*
- * Create the file, elmininating any duplicate.
- */
- SetCursor(*GetCursor(watchCursor));
- status = Create(fileName, vRefNum, creator, 'TEXT');
- if (status == dupFNErr) { /* Exists already? */
- status = FSDelete(fileName, vRefNum);
- if (status == noErr)
- status = Create(fileName, vRefNum, creator, 'TEXT');
- }
- if (status == noErr)
- status = FSOpen(fileName, vRefNum, fileRefNum);
- if (status != noErr)
- *fileRefNum = 0;
- InitCursor();
- return (status);
- }
-
- /*
- * Write one line of text to the output file. Fail on errors.
- */
- void
- WriteAuditOutputLine(
- ConstStr255Param theText,
- short fileRefNum
- )
- {
- long textLength;
- static char gEndOfLine[1] = { kEndOfLine };
-
- textLength = theText[0];
- if (textLength > 0) {
- FailOSErr(
- FSWrite(fileRefNum, &textLength, &theText[1]),
- kErrWriteOutputFile
- );
- }
- textLength = 1;
- FailOSErr(
- FSWrite(fileRefNum, &textLength, gEndOfLine),
- kErrWriteOutputFile
- );
- }
-
- /*
- * Close the Audit output file. If status == noErr, the file is closed normally,
- * Otherwise, the program cleans up after an error. Errors return via the Failure
- * mechanism. (Note: this includes a call with status != noErr.) The I/O model is
- * as follows:
- * TRY {
- * PromptAndCreateAuditOutputFile(...);
- * WriteAuditOutputLine(...);
- * CloseAuditOutputFile(noErr, ...);
- * }
- * CATCH {
- * CloseAuditOutputFile(STATUS, ...);
- * }
- * ENDTRY;
- */
- void
- CloseAuditOutputFile(
- OSErr status,
- short fileRefNum,
- short volumeRefNum,
- ConstStr255Param fileName
- )
- {
- if (status == noErr) {
- TRY {
- FailOSErr(FSClose(fileRefNum), kNoMessage);
- FailOSErr(FlushVol(NULL, volumeRefNum), kNoMessage);
- }
- CATCH {
- status = STATUS;
- NO_PROPAGATE;
- }
- ENDTRY;
- }
- if (status != noErr)
- (void) FSDelete(fileName, volumeRefNum);
- FailOSErr(status, kErrCloseOutputFile);
- }
-